//
//  MYChangePdViewController.m
//  投融保
//
//  Created by 523 on 13-12-18.
//  Copyright (c) 2013年 木叶科技. All rights reserved.
//


#import "MYChangePdViewController.h"
#import "MYWebService.h"

@interface MYChangePdViewController ()

@end

@implementation MYChangePdViewController

@synthesize bgScrollview;
@synthesize txtconfirmPd;
@synthesize txtCurrentPd;
@synthesize txtNewPd;

extern NSString *key;
extern NSString *appKey;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    SETBACKGROUND
    
    [self initView];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void) initView
{
    //顶部账户视图
    Y=0;
    if (IOS_VERSION>=7.0) {
        Y=20;
    }
    strPasswwordType=@"logPassword";
    
    bgScrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 104+Y, 768, SCREEN_HEIGHT-Y-104)];
    bgScrollview.backgroundColor=[UIColor clearColor];
    if (SCREEN_HEIGHT>480) {
        bgScrollview.contentSize=CGSizeMake(768, SCREEN_HEIGHT+100);
    }else
    {
        bgScrollview.contentSize=CGSizeMake(768, SCREEN_HEIGHT+150);
    }
    
    [self.view addSubview:bgScrollview];
    
    
    UIView *titleView=[[UIView alloc] initWithFrame:CGRectMake(0, Y, SCREEN_WIDTH, 104)];
    [titleView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"titile_bg"]]];
    [self.view addSubview:titleView];
    
    
    UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(284, 2, 200, 104-4)];
    [lblTitle setText:@"修改密码"];
    [lblTitle setTextColor:[UIColor whiteColor]];
    [lblTitle setFont:[UIFont boldSystemFontOfSize:40]];
    lblTitle.textAlignment=NSTextAlignmentCenter;
    lblTitle.backgroundColor=[UIColor clearColor];
    [titleView addSubview:lblTitle];
    
    
    UIButton *btnReturn=[UIButton new];
    [btnReturn setFrame:CGRectMake(0, 0, 105, 105)];
    [btnReturn setImage:[UIImage imageNamed:@"return_icon"] forState:UIControlStateNormal];
    [btnReturn.titleLabel setFont:[UIFont systemFontOfSize:22]];
    [btnReturn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnReturn addTarget:self action:@selector(btnReturnClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview: btnReturn];
    
    float point_x=30 ;
    float point_y=Y+104+10;
    
    
    
    NSArray *arrBtnName=[[NSArray alloc] initWithObjects:@"修改登录密码",@"修改支付密码", nil];
    
    UIImageView *btnBgView=[[UIImageView alloc] initWithFrame:CGRectMake(34, 20, 700, 88)];
    btnBgView.image=[UIImage imageNamed:@"viewbg"];
    [bgScrollview addSubview:btnBgView];
    
    for (int i=0; i<=1; i++) {
        UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(34+(350*i), 20, 350, 88)];
        [button setTag:10108+i];
        [button.titleLabel setFont:[UIFont systemFontOfSize:35]];
        [button setTitle:[arrBtnName objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageNamed:@"segmented_left"] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button addTarget: self action:@selector(changgeModifyType:) forControlEvents:UIControlEventTouchUpInside];
        if (i==0) {
            button.selected=YES;
        }
        [bgScrollview addSubview:button];
    
    }
    if (IOS_VERSION<7.0) {
        point_y+=20;
    }
    //当前密码
    UIView *currentPdView=[[UIView alloc] initWithFrame:CGRectMake(point_x, point_y, 700, 88)];
//    [currentPdView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"textfild_bg@2x"]]];
    [bgScrollview addSubview:currentPdView];
    
    UIImageView *currentPdViewBgImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 700, 88)];
    [currentPdViewBgImageView setImage:[UIImage imageNamed:@"textfild_bg"]];
    [currentPdView addSubview:currentPdViewBgImageView];
    
    UILabel *lblCurrentPd=[[UILabel alloc] initWithFrame:CGRectMake(point_x, 24, 120, 40)];
    [lblCurrentPd setText:@"当前密码"];
    lblCurrentPd.textColor =UIColorFromRGB(0x545353);
    lblCurrentPd.font =[UIFont boldSystemFontOfSize:28];
    [currentPdView addSubview:lblCurrentPd];
    
    UIImageView *intervalImage1=[[UIImageView alloc] initWithFrame:CGRectMake(152, 4, 1, 80)];
    intervalImage1.image =[UIImage imageNamed:@"interval"];
    [currentPdView addSubview:intervalImage1];
    
    txtCurrentPd=[[UITextField alloc] initWithFrame:CGRectMake(160, 24, 540, 40)];
    [txtCurrentPd setPlaceholder:@"请输入您的当前密码"];
    txtCurrentPd.font =[UIFont systemFontOfSize:28];
    txtCurrentPd.secureTextEntry=YES;
    [txtCurrentPd addTarget:self action:@selector(closeKeyboar) forControlEvents:UIControlEventEditingDidEndOnExit];
    [currentPdView addSubview:txtCurrentPd];
    
    
    
    
    //新密码
    
    point_y+=98;
    
    UIView *newPdView=[[UIView alloc] initWithFrame:CGRectMake(point_x, point_y, 700, 88)];
//    [newPdView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"textfild_bg@2x"]]];
    [bgScrollview addSubview:newPdView];
    
    UIImageView *newPdViewBgImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 700, 88)];
    [newPdViewBgImageView setImage:[UIImage imageNamed:@"textfild_bg"]];
    [newPdView addSubview:newPdViewBgImageView];
    
    UILabel *lblNewPd=[[UILabel alloc] initWithFrame:CGRectMake(point_x, 24, 120, 40)];
    [lblNewPd setText:@"新  密  码"];
    lblNewPd.textColor =UIColorFromRGB(0x545353);
    lblNewPd.font =[UIFont boldSystemFontOfSize:28];
    [newPdView addSubview:lblNewPd];
    
    UIImageView *intervalImage2=[[UIImageView alloc] initWithFrame:CGRectMake(152, 4, 1, 80)];
    intervalImage2.image =[UIImage imageNamed:@"interval"];
    [newPdView addSubview:intervalImage2];
    
    txtNewPd=[[UITextField alloc] initWithFrame:CGRectMake(160, 24, 540, 40)];
    [txtNewPd setPlaceholder:@"请输入密码"];
    txtNewPd.font =[UIFont systemFontOfSize:28];
    txtNewPd.secureTextEntry=YES;
    txtNewPd.delegate=self;
    [txtNewPd addTarget:self action:@selector(closeKeyboar) forControlEvents:UIControlEventEditingDidEndOnExit];
    [newPdView addSubview:txtNewPd];

    
    //确认密码
    point_y+=98;
    UIView *confirmPdView=[[UIView alloc] initWithFrame:CGRectMake(point_x, point_y, 700, 88)];
//    [confirmPdView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"textfild_bg@2x"]]];
    [bgScrollview addSubview:confirmPdView];
    
    UIImageView *confirmPdViewBgImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 700, 88)];
    [confirmPdViewBgImageView setImage:[UIImage imageNamed:@"textfild_bg"]];
    [confirmPdView addSubview:confirmPdViewBgImageView];
    
    UILabel *lblconfirmPd=[[UILabel alloc] initWithFrame:CGRectMake(point_x, 24, 120, 40)];
    [lblconfirmPd setText:@"确认密码"];
    lblconfirmPd.textColor =UIColorFromRGB(0x545353);
    lblconfirmPd.font =[UIFont boldSystemFontOfSize:28];
    [confirmPdView addSubview:lblconfirmPd];
    
    UIImageView *intervalImage3=[[UIImageView alloc] initWithFrame:CGRectMake(152, 4, 1, 80)];
    intervalImage3.image =[UIImage imageNamed:@"interval"];
    [confirmPdView addSubview:intervalImage3];
    
    txtconfirmPd=[[UITextField alloc] initWithFrame:CGRectMake(160, 24, 540, 40)];
    [txtconfirmPd setPlaceholder:@"请再次输入密码"];
    txtconfirmPd.font =[UIFont systemFontOfSize:28];
    txtconfirmPd.secureTextEntry=YES;
    txtconfirmPd.delegate=self;
    [txtconfirmPd addTarget:self action:@selector(closeKeyboar) forControlEvents:UIControlEventEditingDidEndOnExit];
    [confirmPdView addSubview:txtconfirmPd];

    
    //确认按钮
    
    UIButton *btnSubmit=[[UIButton alloc] initWithFrame:CGRectMake(point_x, 450, 700, 88)];
    [btnSubmit.titleLabel setFont:[UIFont systemFontOfSize:35]];
    [btnSubmit setTitle:@"确认修改" forState:UIControlStateNormal];
    [btnSubmit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnSubmit setBackgroundImage:[UIImage imageNamed:@"login_bg"] forState:UIControlStateNormal];
    [btnSubmit addTarget:self action:@selector(btnSubmitClick) forControlEvents:UIControlEventTouchUpInside];
    [bgScrollview addSubview:btnSubmit];
    
    
}


//返回前页按钮方法
- (void)btnReturnClick
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//关闭键盘
- (void)closeKeyboar
{
    [self.bgScrollview setContentOffset:CGPointMake(0, 0) animated:YES];
    [self.view endEditing:YES];
}

//修改密码事件
- (void)btnSubmitClick
{
//     NSString *strModifiyPassword=@"service=modifyPasswordService&oldPassword=111111&newPassword=666666&newPasswordTo=666666&modifyType=logPassword";
    if ([txtconfirmPd.text isEqualToString:txtNewPd.text]&&(txtNewPd.text.length>=6||txtNewPd.text.length<=20)) {
        NSString *strModifiyPassword=[NSString stringWithFormat:@"service=modifyPasswordService&oldPassword=%@&newPassword=%@&newPasswordTo=%@&modifyType=%@",txtCurrentPd.text,txtNewPd.text,txtconfirmPd.text,strPasswwordType];
        
        NSDictionary *dictChangePd=[[MYWebService new] getDataSynWithKey:key encryptedString:strModifiyPassword appKey:appKey];
        //    NSLog(@"修改结果：%@",dictChangePd);

        
        int code=[[dictChangePd valueForKey:@"code"] intValue];
        
        switch (code) {
            case 1:
            {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"密码修改成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                
                [alert show];
            }
                break;
            case 0:
            {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"原密码错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                
                [alert show];
            }
                break;
                
            default:
            {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"%@",[dictChangePd valueForKey:@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                
                [alert show];
            }
                
                break;
        }

    }else
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"新密码输入有误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
    }
    

    
}


- (void)changgeModifyType:(UIButton *)sender
{
    txtconfirmPd.text=@"";
    txtCurrentPd.text=@"";
    txtNewPd.text=@"";
    switch (sender.tag) {
        case 10108:
            strPasswwordType=@"logPassword";
            ((UIButton *)[self.view viewWithTag:10108]).selected=YES;
            ((UIButton *)[self.view viewWithTag:10109]).selected=NO;
            [((UIButton *)[self.view viewWithTag:10108]) setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [((UIButton *)[self.view viewWithTag:10108]) setBackgroundImage:[UIImage imageNamed:@"segmented_left"] forState:UIControlStateSelected];
            [((UIButton *)[self.view viewWithTag:10109]) setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            
            break;
        case 10109:
            strPasswwordType=@"payPassword";
            ((UIButton *)[self.view viewWithTag:10108]).selected=NO;
            ((UIButton *)[self.view viewWithTag:10109]).selected=YES;
            [((UIButton *)[self.view viewWithTag:10108]) setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [((UIButton *)[self.view viewWithTag:10109]) setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [((UIButton *)[self.view viewWithTag:10109]) setBackgroundImage:[UIImage imageNamed:@"segmented_right"] forState:UIControlStateSelected];
            break;
            
        default:
            break;
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            txtconfirmPd.text=@"";
            txtCurrentPd.text=@"";
            txtNewPd.text=@"";
            break;
            
        default:
            break;
    }
}

#pragma mark +++++++++键盘+++++++++++
int prewTag ;  //编辑上一个UITextField的TAG,
float prewMoveY; //编辑的时候移动的高度
/**
 开始编辑UITextField的方法
 */
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    
    CGRect textFrame =  textField.frame;
    float textY = textFrame.origin.y+textFrame.size.height;
    float bottomY = self.bgScrollview.frame.size.height-textY;
    if(bottomY>=260)  //判断当前的高度是否已经有216，如果超过了就不需要再移动主界面的View高度
    {
        prewTag = -1;
        return;
    }
    prewTag = textField.tag;
    float moveY;
    if (SCREEN_HEIGHT>480) {
        moveY = 260-bottomY;
    }else
    {
        moveY = 300-bottomY;
    }
    
    prewMoveY = moveY;
    [UIView animateWithDuration:0.25 animations:^{
        [self.bgScrollview setContentOffset:CGPointMake(0, prewMoveY) animated:YES];
    }];
    
}

/**
 结束编辑UITextField的方法，让原来的界面还原高度
 */
-(void)textFieldDidEndEditing:(UITextField *)textField
{

    if(prewTag == -1) //当编辑的View不是需要移动的View
    {
        return;
    }
    float moveY ;
    
    if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
    {   //还原界面
        moveY =  prewMoveY;
        [self.bgScrollview setContentOffset:CGPointMake(0, moveY)];
    }
    
}
#pragma mark-   -----------状态栏底色------
- (UIStatusBarStyle)preferredStatusBarStyle
{
    [self.view setBackgroundColor:[UIColor blackColor]];
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
@end
